const app = getApp();
const AJAX = app.$ajax,
  $wx = app.$wx;

Page({
  data: {
    showPopupBttom: false
  },

  onLoad(options){
   this.setData({
     orderDetail: wx.getStorageSync('orderDetail')
   });
  },

  onShareAppMessage(){

  },

  selectDiscountCoupon(){
    this.setData({
      showPopupBttom: true
    });
  },

  getCoupon(e){
    let ed = e.currentTarget.dataset;
    $wx.showToast('已选中一张优惠卷');
    this.setData({
      couponId: ed.id
    });
  },
  
  integralInput(e){
    let td = this.data, val = e.detail.value;
    this.setData({
      integral: val > td.orderDetail.score ? 0 : val
    });
  },

  submit(){
    let td = this.data;
    this.getPay({
      coupon_id: td.couponId,
      score: td.integral
    });
  },

  getPay(params = {}){
    AJAX.pay({
      loadType: 'default',
      loadMsg: '支付中...',
      type: 1,
      pay_type: 0,
      ...params
    }).then(([status, res, msg]) => {
      console.log(res);
      if(status){
        wx.requestPayment({
          timeStamp: res.timestamp,
          nonceStr: res.nonceStr,
          package: res.package,
          signType: res.signType,
          paySign: res.paySign,
          success(res) {
            $wx.showToast('支付成功', {
              success() {
                setTimeout(callback => {
                  wx.navigateBack();
                }, 2000);
              }
            });
          },
          fail(res) {
            $wx.showToast('支付失败');
          }
        });
      }else{
        $wx.showToast('支付失败');
      }
    });
  }
});